using System.Collections;
using I2.Loc;
using UnityEngine;

public class FloatingGatePointer : MonoBehaviour
{
	[SerializeField]
	private UILabel gateName;

	[SerializeField]
	private UILabel message;

	[SerializeField]
	private UISprite healthSprite;

	[SerializeField]
	private UISprite gateSprite;

	[SerializeField]
	private UISprite hitImage;

	private int _initialWidth = -1;

	private void Start()
	{
		_initialWidth = healthSprite.width;
	}

	public void Init(string gateName)
	{
		if (_initialWidth != -1)
		{
			this.gateName.text = gateName;
			if (TDMController.myTeam == PunTeams.Team.blue)
			{
				message.text = ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_gatesDestroy");
			}
			else if (TDMController.myTeam == PunTeams.Team.red)
			{
				message.text = ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_gatesDefend");
			}
			else
			{
				message.text = "NONE";
			}
			message.gameObject.SetActive(false);
			healthSprite.gameObject.SetActive(true);
			healthSprite.width = _initialWidth;
		}
	}

	public void ShowHit()
	{
		CopsAndRobbersInterface.instance.StartCoroutine(ShowHitCoroutine());
	}

	private IEnumerator ShowHitCoroutine()
	{
		hitImage.gameObject.SetActive(true);
		yield return new WaitForSeconds(0.23f);
		hitImage.gameObject.SetActive(false);
	}

	public void ShowMessage()
	{
		message.gameObject.SetActive(true);
	}

	public void ShowHealth()
	{
		healthSprite.gameObject.SetActive(true);
		message.gameObject.SetActive(false);
	}

	public void SetHealth(int health, int maxHealth)
	{
		if ((float)maxHealth != 0f && _initialWidth != -1)
		{
			int width = healthSprite.width;
			int num = Mathf.CeilToInt((float)health / (float)maxHealth * (float)_initialWidth);
			if (health <= maxHealth)
			{
				healthSprite.fillAmount = (float)health / (float)maxHealth;
			}
		}
	}

	private void Update()
	{
		CRGate currentGate = CopsAndRobbersController.instance.GetCurrentGate();
		if (!(currentGate == null))
		{
			float num = Vector3.Distance(PlayerBehavior.MyPlayer.transform.position, currentGate.transform.position);
			float distance = ((CopsRobbersSettings)settings.GetSettings(TypeGame.CopsAndRobbers)).distance;
			if (num <= distance)
			{
				ShowHealth();
			}
			else
			{
				ShowMessage();
			}
		}
	}
}
